package com.maya.medicalwastes.ui;

import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.maya.medicalwastes.ParamConstant;
import com.maya.medicalwastes.R;
import com.maya.medicalwastes.adapter.TadayCollectAdapter;
import com.maya.medicalwastes.adapter.WareHouseListAdapter;
import com.maya.medicalwastes.adapter.WareItemAdapter;
import com.maya.medicalwastes.bean.WareHouseItemBean;
import com.maya.medicalwastes.dialog.LoadingDialog;
import com.maya.medicalwastes.entity.CollectBean;
import com.maya.medicalwastes.entity.KeyBean;
import com.maya.medicalwastes.entity.WaitWareBean;
import com.maya.medicalwastes.httpNet.ApiService;
import com.maya.medicalwastes.httpNet.HttpService;
import com.maya.medicalwastes.httpNet.callback.ResponseCallBack;
import com.maya.medicalwastes.httpNet.common.SharedPreferencesConstant;
import com.maya.medicalwastes.httpNet.manage.RetrofitManager;
import com.maya.medicalwastes.httpNet.model.ResultModel;
import com.maya.medicalwastes.util.DateUtils;
import com.maya.medicalwastes.util.PrefUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import io.reactivex.Observable;

/**
 * @author created by fuguoyong
 * 版本：1.0
 * 创建日期：2020-11-19 15
 * 描述：CollectActivity
 */
public class InWareHousetActivity extends AppCompatActivity {

    @BindView(R.id.recyclerview)
    RecyclerView recyclerView;
    @BindView(R.id.Warehouse)
    RecyclerView mWarehouse;
    @BindView(R.id.tv_carSameDayNum)
    TextView tv_carSameDayNum;
    @BindView(R.id.tv_carSameDayWeight)
    TextView tv_carSameDayWeight;
    @BindView(R.id.tv_title)
    TextView tv_title;
    @BindView(R.id.type)
    RecyclerView typeRecy;
    List<CollectBean> mlist = new ArrayList<>();
    List<KeyBean> keyBeans = new ArrayList<>();
    List<WareHouseItemBean> mHouseList = new ArrayList<>();
    TadayCollectAdapter collectAdapter;
    WareItemAdapter wareItemAdapter;
    WareHouseListAdapter mWareHouseListAdapter;
    String mId;
    String keyWord = "";
    private LoadingDialog mShow;
    String type;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_inwarehouse);
        ButterKnife.bind(this);

        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        collectAdapter = new TadayCollectAdapter(R.layout.list_taday_collect_item, mlist);
        recyclerView.setAdapter(collectAdapter);

        mWarehouse.setLayoutManager(new LinearLayoutManager(this));
        mWareHouseListAdapter = new WareHouseListAdapter(R.layout.list_waste_house_item, mHouseList);
        mWarehouse.setAdapter(mWareHouseListAdapter);
        mWareHouseListAdapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                if (view.getId() == R.id.tv_type) {
                    for (WareHouseItemBean bean : mHouseList) {
                        bean.setCheck(false);
                    }
                    mHouseList.get(position).setCheck(true);
                    mWareHouseListAdapter.notifyDataSetChanged();
                    mId = mHouseList.get(position).getId();
                    queryByPage(mHouseList.get(position).getId(), keyWord);
                }
            }
        });
        LinearLayoutManager manager = new LinearLayoutManager(this);
        manager.setOrientation(RecyclerView.HORIZONTAL);
        typeRecy.setLayoutManager(manager);
        wareItemAdapter = new WareItemAdapter(R.layout.list_waste_type_item, keyBeans);
        typeRecy.setAdapter(wareItemAdapter);
        wareItemAdapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                if (view.getId() == R.id.tv_type) {
                    for (KeyBean bean : keyBeans) {
                        bean.setCheck(false);
                    }
                    keyBeans.get(position).setCheck(true);
                    wareItemAdapter.notifyDataSetChanged();
                    keyWord = wareItemAdapter.getItem(position).getKey();
                    queryByPage(mId, wareItemAdapter.getItem(position).getKey());
                }
            }
        });
        keySearch();
        //初始化仓库信息
        queryWarehouseBy();
    }

    @Override
    protected void onResume() {
        super.onResume();
        Bundle bundle = getIntent().getExtras();
        if (bundle != null) {
            type = bundle.getString("type", "");
        }
        tv_title.setText(type.equals("3")?"当日库存":"当日出库单");
    }

    private void queryByPage(String id, String key) {
        HashMap<String, Object> param = new HashMap<>();
        param.put("warehouseId", id);
        param.put("warehouseState", type);
        param.put("garbageCategory", key);
        param.put("createTime", DateUtils.stringToDate(System.currentTimeMillis(), "yyyy-MM-dd"));
        Observable<ResultModel<WaitWareBean>> call = RetrofitManager.getInstance().getRetrofit().create(ApiService.class).currentStock(param, PrefUtils.getString(this, SharedPreferencesConstant.TOKEN, ""));
        HttpService.getInstance().enqueue(call, new ResponseCallBack<ResultModel<WaitWareBean>>() {
            @Override
            public void onResponse(ResultModel<WaitWareBean> response) {
                if (response.httpCode == 200) {
                    collectAdapter.replaceData(response.data.getList());
                    tv_carSameDayNum.setText(response.data.getSummary().getNum() + "件");
                    tv_carSameDayWeight.setText(response.data.getSummary().getSumWeight() + "kg");
                }

            }

            @Override
            public void onFailure(int httpCode) {
            }
        }, this);


    }


    public void keySearch() {
        HashMap<String, Object> param = new HashMap<>();
        param.put("key", "CATEGORY");
        param.put(ParamConstant.TOKEN, PrefUtils.getString(this, SharedPreferencesConstant.TOKEN, ""));
        Observable<ResultModel<ArrayList<KeyBean>>> call = RetrofitManager.getInstance().getRetrofit().create(ApiService.class).Keyword(param);
        HttpService.getInstance().enqueue(call, new ResponseCallBack<ResultModel<ArrayList<KeyBean>>>() {
            @Override
            public void onResponse(ResultModel<ArrayList<KeyBean>> response) {
                if (response != null && response.httpCode == 200) {
                    response.data.add(0, new KeyBean("", "全部", true));
//                    keyBeans.addAll(response.data);
                    wareItemAdapter.replaceData(response.data);

                }
            }

            @Override
            public void onFailure(int httpCode) {
            }
        }, this);


    }

    public void queryWarehouseBy() {
        mShow = LoadingDialog.show(this);
        HashMap<String, Object> param = new HashMap<>();
        param.put("organId", "200");
        param.put(ParamConstant.TOKEN, PrefUtils.getString(this, SharedPreferencesConstant.TOKEN, ""));
        Observable<ResultModel<ArrayList<WareHouseItemBean>>> call = RetrofitManager.getInstance().getRetrofit().create(ApiService.class).queryWarehouseBy(param);
        HttpService.getInstance().enqueue(call, new ResponseCallBack<ResultModel<ArrayList<WareHouseItemBean>>>() {
            @Override
            public void onResponse(ResultModel<ArrayList<WareHouseItemBean>> response) {
                mShow.dismiss();
                if (response != null && response.httpCode == 200) {
                    if (response.data.size() > 0) {
                        List<WareHouseItemBean> mList = response.data;
                        WareHouseItemBean mWareHouseItemBean = mList.get(0);
                        mId = mWareHouseItemBean.getId();
                        mWareHouseItemBean.setCheck(true);
                        mList.set(0, mWareHouseItemBean);
                        mWareHouseListAdapter.replaceData(mList);
                        queryByPage(mId, "");

                    }
                }
            }

            @Override
            public void onFailure(int httpCode) {
                mShow.dismiss();
            }
        }, this);


    }


}
